package gui;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JPanel;

/********************************************************
 *
 *  Project :  TeamProject
 *  Package :  gui
 *  File    :  ImagePanel.java
 *  Name    :  Tyler Daines
 *  Date    :  Nov 12, 2013
 *
 ********************************************************/
public class ImagePanel extends JPanel
{
	private BufferedImage image;
	private String imagePath, nullPath;
	
	/********************************************************
	 * Name       : ImagePanel
	 * Purpose    : Constructor
	 * Parameters : 
	 ********************************************************/
	public ImagePanel(String imagePath)
	{
		nullPath = "/resources/recipes.png";
		this.imagePath = imagePath;
		if (!imagePath.equals(nullPath) && imagePath != null)
		{
			try
			{
				File file = new File(imagePath);
				image = ImageIO.read(file);
				this.setMinimumSize(new Dimension(image.getWidth(), image.getHeight()));
			}
			catch (IOException e)
			{
				if(e.getMessage().equals("Can't read input file!"))
				{
					try
					{
						image = ImageIO.read(getClass().getResource(nullPath));
						this.setMinimumSize(new Dimension(image.getWidth(), image.getHeight()));
					}
					catch (IOException e1)
					{
						System.err.println(e1.getMessage());
					}
				}
						
			}
		}
		else
		{
			try
			{
				image = ImageIO.read(getClass().getResource(nullPath));
				this.setMinimumSize(new Dimension(image.getWidth(), image.getHeight()));
			}
			catch (IOException e)
			{
				System.err.println(e.getMessage());
			}
		}
		
	}
	
	@Override
	public void paint(Graphics g)
	{
		super.paint(g);
		g.drawImage(image, 0, 0, null);
	}
	
	public String getImagePath()
	{
		return imagePath;
	}
	
	public void setImagePath(String imagePath)
	{
		this.imagePath = imagePath;
		try
		{
			File file = new File(imagePath);
			image = ImageIO.read(file);
			this.setMinimumSize(new Dimension(image.getWidth(), image.getHeight()));
		}
		catch (IOException e)
		{
			if(e.getMessage().equals("Can't read input file!"))
			{
				try
				{
					image = ImageIO.read(getClass().getResource(nullPath));
					this.setMinimumSize(new Dimension(image.getWidth(), image.getHeight()));
				}
				catch (IOException e1)
				{
					System.err.println(e1.getMessage());
				}
			}
		}
	}
}
